﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class PhieuXuatKho
    {

        public int MaPXK { get; set; }

        public string MaPXKHienThi
        {
            get
            {

                return "PXK" + MaPXK;
            }
        }
        public int MaHDB { get; set; }
        public string MaHDBHienThi
        {
            get
            {
                return "HDB" + MaHDB;
            }
        }
        public int MaNV { get; set; }
        public string MaNVHienThi
        {
            get
            {

                return "NV" + MaNV;
            }
        }
        public DateTime NgayXuatHD { get; set; }

        public string LyDoXuat { get; set; }
        public string TenKhachHang { get; set; }

        public string DiaChiKhachHang { get; set; }
        public string DienThoai { get; set; }

        public List<ChiTietHDBan> DanhSachChiTietMatHang { get; set; }
        //public HangHoa hangHoa {get;set;}



        public PhieuXuatKho(int maHDB, string tenKhachHang,
            string diaChi, string dienthoai)
        {
            this.MaPXK = 0;
            this.MaHDB = maHDB;
            this.LyDoXuat = "Xuất theo đơn: HDB" + maHDB;
            this.TenKhachHang = tenKhachHang;
            this.DiaChiKhachHang = diaChi;
            this.DienThoai = dienthoai;
        }

        public PhieuXuatKho(int maPXK, int maHDB, int maNV, DateTime ngayXuat,
            string lyDoXuat, string tenKhachHang, string diaChi, string dienthoai)
        {
            this.MaPXK = maPXK;
            this.MaHDB = maHDB;
            this.MaNV = maNV;
            this.NgayXuatHD = ngayXuat;
            this.LyDoXuat = lyDoXuat;
            this.TenKhachHang = tenKhachHang;
            this.DiaChiKhachHang = diaChi;
            this.DienThoai = dienthoai;
        }



        public static PhieuXuatKho TaoPhieuXuatKho(int maHDB)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuXuatKho phieuXuatKho;
            try
            {
                phieuXuatKho = (from p in db.PhieuXuatKhos
                                where p.MaHDB == maHDB
                                select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).Single();
            }
            catch
            {
                phieuXuatKho = (from p in db.HoaDonBans
                                where p.MaHDB == maHDB
                                select new PhieuXuatKho(p.MaHDB, p.DonDatKH.KhachHang.TenKH, p.DonDatKH.KhachHang.DiaChi, p.DonDatKH.KhachHang.DienThoai)).Single();
            }

            List<ChiTietHDBan> danhsachChiTietMatHang = (from p in db.ChiTietHDBans
                                                         where p.MaHDB == maHDB
                                                         select new ChiTietHDBan(p.MaCTHDB, p.MaHDB, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaBan, p.ChietKhau)).ToList();
            if (danhsachChiTietMatHang == null || danhsachChiTietMatHang.Count == 0)
                danhsachChiTietMatHang = new List<ChiTietHDBan>();

            phieuXuatKho.DanhSachChiTietMatHang = danhsachChiTietMatHang;

            return phieuXuatKho;
        }

        public static PhieuXuatKho LayPhieuXuatKho(int maPXK)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuXuatKho phieuXuatKho = (from p in db.PhieuXuatKhos
                                         where p.MaPXK == maPXK
                                         select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).Single();

            List<ChiTietHDBan> danhsachChiTietMatHang = (from p in db.ChiTietHDBans

                                                         where p.MaHDB == phieuXuatKho.MaHDB
                                                         select new ChiTietHDBan(p.MaCTHDB, p.MaHDB, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaBan, p.ChietKhau)).ToList();
            if (danhsachChiTietMatHang == null || danhsachChiTietMatHang.Count == 0)
                danhsachChiTietMatHang = new List<ChiTietHDBan>();


            phieuXuatKho.DanhSachChiTietMatHang = danhsachChiTietMatHang;

            return phieuXuatKho;
        }

        public static List<PhieuXuatKho> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.PhieuXuatKhos
                    select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).ToList();
        }



        public static int ThemMoi(PhieuXuatKho phieuXuatKho)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.PhieuXuatKho tamObject = new DataAccess.PhieuXuatKho();
            tamObject.MaNV = phieuXuatKho.MaNV;
            tamObject.MaHDB = phieuXuatKho.MaHDB;

            tamObject.NgayXuat = phieuXuatKho.NgayXuatHD;

            tamObject.LyDoXuat = phieuXuatKho.LyDoXuat;

            db.PhieuXuatKhos.InsertOnSubmit(tamObject);

            for (int i = 0; i < phieuXuatKho.DanhSachChiTietMatHang.Count; i++)
            {
                int mahang = phieuXuatKho.DanhSachChiTietMatHang[i].MaHang;
                var hangHoa = (from p in db.HangHoas
                               where p.MaHang == mahang
                               select p).Single();
                int soLuongXuat = phieuXuatKho.DanhSachChiTietMatHang[i].SoLuong;
                if (soLuongXuat > 0 && soLuongXuat < hangHoa.SoLuongTon)
                {
                    hangHoa.SoLuongTon = hangHoa.SoLuongTon - soLuongXuat;
                }
            }
            db.SubmitChanges();

            return tamObject.MaPXK;
        }

        public static List<PhieuXuatKho> LayDanhSach_MaHoaDonBan(string maHDBHienThi)
        {
            try
            {
                int maHDB = Int16.Parse(maHDBHienThi.Substring(3));

                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuXuatKhos
                        where p.MaHDB == maHDB
                        select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static void CapNhat(PhieuXuatKho phieuXuatKho)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.PhieuXuatKhos
                             where p.MaPXK == phieuXuatKho.MaPXK
                             select p).Single();

            tamObject.LyDoXuat = phieuXuatKho.LyDoXuat;

            tamObject.NgayXuat = phieuXuatKho.NgayXuatHD;

            for (int i = 0; i < phieuXuatKho.DanhSachChiTietMatHang.Count; i++)
            {
                int mahang = phieuXuatKho.DanhSachChiTietMatHang[i].MaHang;
                var hangHoa = (from p in db.HangHoas
                               where p.MaHang == mahang
                               select p).Single();
                int soLuongXuat = phieuXuatKho.DanhSachChiTietMatHang[i].SoLuong;
                if (soLuongXuat > 0 && soLuongXuat < hangHoa.SoLuongTon)
                {
                    hangHoa.SoLuongTon = hangHoa.SoLuongTon - soLuongXuat;
                }
            }

            db.SubmitChanges();
        }


        public static List<PhieuXuatKho> LayDanhSach_NgayLapPhieu(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuXuatKhos
                        where p.NgayXuat.Date >= tungay && p.NgayXuat.Date <= denngay
                        select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<PhieuXuatKho> LayDanhSach_MaPhieuXuat(string maPXKHienThi)
        {
            try
            {
                int maPXK = Int16.Parse(maPXKHienThi.Substring(3));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                return (from p in db.PhieuXuatKhos
                        where p.MaPXK == maPXK
                        select new PhieuXuatKho(p.MaPXK, p.MaHDB, p.MaNV, p.NgayXuat, p.LyDoXuat, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }


    }
}
